package org.chengn.level_middle.context2807;

import org.chengn.definition.ListNode;

/**
 * @Title: Context2807
 * @Author ChengN
 * @Package org.chengn.level_middle.context2807
 * @Date 2024/1/6 1:47
 * @description: 在链表中插入最大公约数
 */
public class Context2807 {
    public ListNode insertGreatestCommonDivisors(ListNode head) {
        if (head.next == null) return head;
        ListNode nextNode = insertGreatestCommonDivisors(head.next);
        head.next = new ListNode(gcd(head.val, nextNode.val), nextNode);
        return head;
    }

    private int gcd(int a, int b) {
        return b > 0 ? gcd(b, a % b) : a;
    }
}
